package com.drinks.yiniang.ui.activity.community;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.community.CommunityGoodsAdapter;
import com.drinks.yiniang.common.recycler.decoration.RecommendGoodsItemDecoration;
import com.drinks.yiniang.entity.community.CommunityDetailJson;
import com.drinks.yiniang.ui.widget.layout.CommunityGoodsBottomLayout;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区详情关联的商品列表页面
 *
 * @author han
 * @date 2019/09/10
 */
public class CommunityGoodsActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.OnItemClickListener {

    //nav
    private NavigationBarLayout nav_bar_community_goods;
    private BaseRecyclerView rcv_community_goods;
    private CommunityGoodsBottomLayout ll_community_goods_bottom;

    private CommunityGoodsAdapter mCommunityGoodsAdapter;

    @Override
    protected void initView() {
        nav_bar_community_goods = findViewById(R.id.nav_bar_community_goods);

        rcv_community_goods = findViewById(R.id.rcv_community_goods);

        ll_community_goods_bottom = findViewById(R.id.ll_community_goods_bottom);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
    }

    private void initAdapter() {
        List<CommunityDetailJson.CommunityDetailGoods> ls = getIntent() != null ? (List<CommunityDetailJson.CommunityDetailGoods>) getIntent().getSerializableExtra(Constants.KeyObject) : null;
        rcv_community_goods.setLayoutManager(new LinearLayoutManager(mContext));

        mCommunityGoodsAdapter = new CommunityGoodsAdapter(mContext, ls != null ? ls : new ArrayList<CommunityDetailJson.CommunityDetailGoods>());
        mCommunityGoodsAdapter.setCenterEmpty(getLayoutInflater(), rcv_community_goods);
        mCommunityGoodsAdapter.bindToRecyclerView(rcv_community_goods);
    }

    @Override
    protected void initListener() {
        nav_bar_community_goods.setOnNavgationBarClickListener(this);
        ll_community_goods_bottom.setChildViewClickListener(this);
        mCommunityGoodsAdapter.setOnItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_common_check_all://全选
                setCheckAll(!ll_community_goods_bottom.isCheckAll());
                break;
            case R.id.btn_common_add_cart://加入购物车
                addToCart();
                break;
            case R.id.btn_common_buy://立即购买

                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_community_goods;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        setChecked(mCommunityGoodsAdapter.getItem(position), position);
    }

    private void addToCart() {
        String ids = formatCardId();

        if (StringUtils.isEmpty(ids)) {
            return;
        }


    }

    //获取已选择的商品cartId
    private String formatCardId() {
        if (CommonUtils.isEmptyList(mCommunityGoodsAdapter.getData())) {
            return "";
        }

        StringBuilder stringBuilder = new StringBuilder();

        List<String> mIds = new ArrayList<>();

        CommunityDetailJson.CommunityDetailGoods data;

        //遍历问题id集合和答案集合拼接字符串 作为单数请求服务器
        for (int i = 0; i < mCommunityGoodsAdapter.getData().size(); i++) {
            data = mCommunityGoodsAdapter.getItem(i);

            mIds.add(data.getId());
        }

        for (int i = 0; i < mIds.size(); i++) {
            stringBuilder.append(mIds.get(i));

            if (i < mIds.size() - 1) {
                stringBuilder.append(",");
            }
        }

        return stringBuilder.toString();
    }


    //全选或取消全选
    private void setCheckAll(boolean isCheckAll) {
        if (CommonUtils.isEmptyList(mCommunityGoodsAdapter.getData())) {
            return;
        }

        for (int i = 0; i < mCommunityGoodsAdapter.getData().size(); i++) {
            mCommunityGoodsAdapter.getData().get(i).setChecked(isCheckAll);
        }

        ll_community_goods_bottom.setCheckAll(isCheckAll);
        mCommunityGoodsAdapter.notifyDataSetChanged();
        ll_community_goods_bottom.setButtonEnable(isCheckAll);
    }

    //设置选中
    private void setChecked(CommunityDetailJson.CommunityDetailGoods data, int pos) {
        if (data == null) {
            return;
        }

        data.setChecked(!data.isChecked());

        mCommunityGoodsAdapter.setData(pos, data);

        boolean isCheckAll = true;
        boolean hasChooseGoods = false;

        CommunityDetailJson.CommunityDetailGoods dataObj;

        for (int i = 0; i < mCommunityGoodsAdapter.getData().size(); i++) {
            dataObj = mCommunityGoodsAdapter.getItem(i);

            if (!dataObj.isChecked()) {
                isCheckAll = false;
            } else {
                hasChooseGoods = true;
            }
        }

        ll_community_goods_bottom.setCheckAll(isCheckAll);
        ll_community_goods_bottom.setButtonEnable(hasChooseGoods);
    }
}
